//
//  WBNavigationViewController.swift
//  weibo
//
//  Created by 胡古古 on 2017/6/4.
//  Copyright © 2017年 hugugu. All rights reserved.
//

import UIKit

class WBNavigationViewController: UINavigationController,UIGestureRecognizerDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()

        self.interactivePopGestureRecognizer?.delegate = self
        
    }

    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        
        if viewControllers.count > 0 {
            
            if viewControllers.count == 1 {
                
                let title = viewControllers[0].title!
                
                viewController.navigationItem.leftBarButtonItem = UIBarButtonItem(target: self, action: #selector(clickLeftButton), title: title, imageName: "navigationbar_back_withtext")
                
            } else {
                
                let title = "返回"
                
                viewController.navigationItem.leftBarButtonItem = UIBarButtonItem(target: self, action: #selector(clickLeftButton), title: title, imageName: "navigationbar_back_withtext")

            }
            
            viewController.title = "当前显示的是第\(viewControllers.count + 1)级控制器"
            viewController.hidesBottomBarWhenPushed = true
        }
        
        super.pushViewController(viewController, animated: animated)
        
    }
    
    @objc private func clickLeftButton() {
        
        self.popViewController(animated: true)
        
    }
    
    // 边缘手势的代理
    func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        
        if viewControllers.count == 1 {
        
            return false
        }
        
        return true
     }
    
}
